<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<form id="form_power_input" class="form-horizontal">
  <input type="hidden" name="keyId" id="keyId" value="${keyId}"/>
  <input type="hidden" name="_departmentId" id="_departmentId" value="${departmentId}"/>
  <div id="container_power_input"></div>
</form>

<script id="templates_power_input" type="text/x-jsrender">

  <div class="form-group">
    <label class="col-md-3 control-label">部门</label>
    <div class="col-md-7">
      <input type="hidden" name="departmentId" id="departmentId" value="{{:departmentId}}"/>
      <input type="text" name="departmentName" id="departmentName" value="{{:departmentName}}" class="form-control" disabled>
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label">岗位</label>
    <div class="col-md-7">
      <select name="postId" id="postId" data-live-search="true" class="selectpicker form-control" title="请选择岗位">
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label">上级职权</label>
    <div class="col-md-7">
      <input type="text" name="parentId" id="parentId" class="form-control treepicker" title="请选择上级职权"/>
    </div>
  </div>

</script>

<script id="templates_post_select" type="text/x-jsrender">

  <option data-subtext="{{:description}}" data-tokens="{{:pinYinHead}} {{:pinYin}} {{:name}}" value="{{:id}}">{{:name}}</option>

</script>

<script>

  $(function(){

    // 加载数据
    fn_power_input_load();

  });

  // 初始化表单数据
  function fn_power_input_load() {
    var $templates = $.templates('#templates_power_input');
    var $container = $('#container_power_input');
    var keyId = $('#keyId').val();
    var departmentId = $('#_departmentId').val();

    var url;
    var data;
    if (keyId) {
      url = '<%=path%>/main/power/read/' + keyId;
    } else {
      url = '<%=path%>/main/power/init';
      if (departmentId) {
        data = {departmentId: departmentId};
      }
    }
    __ajax_get(url, data, function(data) {
      if (data.state == 1) {
        var entity = data.data;

        // 初始化模板
        var html = $templates.render(entity);
        $container.html(html);

        // 表单数据校验
        fn_power_input_validate();

        // 岗位下拉
        fn_power_post_list_load(function () {
          if (entity.postId) {
            // 默认选中
            $('#postId').selectpicker('val', entity.postId);
          }
        });

        // 上级职权
        fn_power_parent_tree_load(function () {
          if (entity.parentId) {
            // 默认选中
            $('#parentId').treepicker('val', entity.parentId);
          }
        });

      }
    });
  }

  // 岗位
  function fn_power_post_list_load(success) {
    $('#postId').selectpicker({
      style: 'btn-white'
    });

    var url = '<%=path%>/main/post/list_select';
    var $templates = $('#templates_post_select');
    var $container = $('#postId');
    __ajax_get(url, null, function(data) {
      if (data.state == 1) {
        var list = data.data;

        // 初始化模板
        var html = $templates.render(list);
        $container.html(html);
        $container.selectpicker('refresh');

        // 回调
        success();
      }
    });
  }

  // 上级职权
  function fn_power_parent_tree_load(success) {
    $('#parentId').treepicker({
      style: 'btn-white'
    });

    var keyId = $('#keyId').val();
    var departmentId = $('#_departmentId').val();
    var url = '<%=path%>/main/power/tree_level';
    var data;
    if (keyId) {
      data = {powerId: keyId};
    } else {
      data = {departmentId: departmentId};
    }
    __ajax_get(url, data, function(data) {
      if (data.state == 1) {
        var list = data.data;

        // 初始化
        $('#parentId').treepicker('setTreeNodes', list);

        // 回调
        success();
      }
    });
  }

  // 表单数据校验
  function fn_power_input_validate() {
    __init_validate('form_power_input', {
      rules : {
        postId : {
          required : true
        }
      },
      messages : {
        postId : {
          required : '请选择岗位'
        }
      }
    });
  }

  function fn_power_input_save(callback) {
    if (!$('#form_power_input').valid()) {
      return false;
    }
    __confirm_dialog(null, '确定提交么?',
            function () {
              __form_save('form_power_input', '<%=path%>/main/power/save', function(data) {
                if(data.state == 1){

                  callback(data.data);
                } else {
                  callback();
                }
              });
            },
            function () {
              callback();
            }
    );
  }

</script>